package main

import (
	"fmt"
	"reflect"
	"time"
)

type registerStructMaps map[string]reflect.Type

func (rsm registerStructMaps) Register(name string, c interface{}) {
	rsm[name] = reflect.TypeOf(c).Elem()
}

func (rsm registerStructMaps) RegisterType(name string, c reflect.Type) {
	rsm[name] = c.Elem()
}

func (rsm registerStructMaps) New(name string) (c interface{}, err error) {
	if v, ok := rsm[name]; ok {
		c = reflect.New(v).Interface()
	} else {
		err = fmt.Errorf("not found %s struct", name)
	}
	return
}

type object struct {
	Name string
}

func (obj *object) SetName(value string) {
	obj.Name = value
}

func main() {
	t1 := time.Now()
	rsm := registerStructMaps{}

	rsm.Register("object", (*object)(nil))
	for i := 1; i < 1000000; i++ {
		obj11, _ := rsm.New("object")
		obj := obj11.(*object)
		obj.SetName("反射对象中的方法")
	}
	elapsed := time.Since(t1)
	fmt.Println("总的耗时:", elapsed)

}
